<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="请输入租户uid" prop="name" label-width="110px">
        <el-input
          v-model="queryParams.name"
          placeholder="租户uid"
          clearable
          @clear="handleQuery"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['tenant:ai:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tenantAiList">
      <el-table-column label="租户名称" align="center" prop="tenantName" />
      <el-table-column label="是否全选" align="center" prop="isAll" :formatter="statusFormat"/>
      <el-table-column label="大模型" align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.modelAppInfoDtoList != null">
            <div v-for="(item, index) in scope.row.modelAppInfoDtoList" :key="index" :value="item">
              <el-tag type="success">{{item.name}}</el-tag>
            </div>
          </div>
          <div v-if="scope.row.modelAppInfoDtoList == null">
            <el-tag type="success">全部</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="aiui配置" align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.aiUiConfigDtoList != null">
            <div v-for="(item, index) in scope.row.aiUiConfigDtoList" :key="index" :value="item">
              <el-tag type="success">{{item.name}}</el-tag>
            </div>
          </div>
          <div v-if="scope.row.aiUiConfigDtoList == null">
            <el-tag type="success">全部</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="人设" align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.humanModelManagerDtoList != null">
            <div v-for="(item, index) in scope.row.humanModelManagerDtoList" :key="index" :value="item">
              <el-tag type="success">{{item.name}}</el-tag>
            </div>
          </div>
          <div v-if="scope.row.humanModelManagerDtoList == null">
            <el-tag type="success">全部</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <div     v-if="scope.row.tenantUid != '0'">
            <el-button
              size="mini"
              type="text"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['tenant:ai:edit']"
            >编辑</el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleDelete(scope.row)"
              v-hasPermi="['tenant:ai:remove']"
            >删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改租户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="400px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="租户名称:" label-width="110px" prop="tenantUid">
          <el-select
            v-model="form.tenantUid"
            filterable
            remote
            reserve-keyword
            placeholder="请输入租户名称"
            :remote-method="queryTenantSearch"
            :loading="loading">
            <el-option
              v-for="item in this.tenantList"
              :key="item.uid"
              :label="item.name"
              :value="item.uid">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="大模型:" label-width="110px">
          <el-select
            multiple
            v-model="form.modelUidList"
            filterable
            reserve-keyword
            remote
            placeholder="请输入大模型名称"
            :remote-method="queryModel"
            :loading="loading">
            <el-option
              v-for="item in this.modelList"
              :key="item.uid"
              :label="item.name"
              :value="item.uid">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="aiui配置:" label-width="110px">
          <el-select
            multiple
            v-model="form.aiuiUidList"
            filterable
            remote
            reserve-keyword
            placeholder="请输入aiui名称"
            :remote-method="queryAiUiSearch"
            :loading="loading">
            <el-option
              v-for="item in this.aiuiList"
              :key="item.uid"
              :label="item.name"
              :value="item.uid">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="人设:" label-width="110px">
          <el-select
            multiple
            v-model="form.humanUidList"
            filterable
            remote
            reserve-keyword
            placeholder="请输入人设名称"
            :remote-method="queryHumanSearch"
            :loading="loading">gfc[
            <el-option
              v-for="item in this.humanList"
              :key="item.uid"
              :label="item.name"
              :value="item.uid">
              <template slot="default" slot-scope="scope">
                <span>{{item.name}}</span>
              </template>
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="margin-top: -50px">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listTenantAuthAi, insertTenantAuthAi, deleteTenantAuthAi, selectTenantAuthAi, updateAiUiConfig} from "@/api/tenant/ai/config";
import {listTenant} from "@/api/tenant/enter/tenant";
import {listModelAppInfo} from "@/api/platform/model/model";
import {listAiUiConfig} from "@/api/platform/aiui/config";
import {listHumanModelManager} from "@/api/platform/human/human";


export default {
  name: "TenantAiConfig",
  data() {
    return {
      operationType: true,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      tenantAiList: [],
      tenantList: [],
      modelList: [],
      aiuiList: [],
      humanList:[],
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      detailOpen: false,
      // 是否显示添加修改弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        tenantUid: [
          { required: true, message: "应用名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询模板列表 */
    getList() {
      this.loading = true;
      listTenantAuthAi(this.queryParams).then(response => {
        this.tenantAiList = response.rows;
        this.total = response.total;
        this.loading = false;
      })
    },
    queryTenantSearch(queryString, cb){
      if(queryString != null){
        let data = {}
        data.name = queryString;
        listTenant(data).then(response => {
          this.tenantList = response.rows;
        })
      }
    },
    queryModel(queryString, cb){
      if(queryString != null){
        let data = {}
        data.name = queryString;
        listModelAppInfo(data).then(response => {
          this.modelList = response.rows;
        })
      }
    },
    queryAiUiSearch(queryString, cb){
      if(queryString != null){
        let data = {}
        data.name = queryString;
        listAiUiConfig(data).then(response => {
          this.aiuiList = response.rows;
        })
      }
    },

    queryHumanSearch(queryString, cb){
      if(queryString != null){
        let data = {}
        data.name = queryString;
        listHumanModelManager(data).then(response => {
          this.humanList = response.rows;
        })
      }
    },

    handleTagClose(index){
      this.modelList.splice(index, 1);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.tenantList= [];
      this.modelList=[];
      this.aiuiList=[];
      this.humanList=[];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加租户授权";

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.title = "修改权限";
      selectTenantAuthAi(row.uid).then(response =>{
        this.form = response.data;
        this.aiuiList = this.form.aiUiConfigDtoList;
        this.modelList = this.form.modelAppInfoDtoList;
        this.humanList = this.form.humanModelManagerDtoList;
        this.tenantList = this.form.tenantDtoList;
      })

    },
    //table表格格式化
    statusFormat(row, index){
      if (row.isAll == 0) {
        return "非全选";
      } else if(row.isAll == 1){
        return "全选";
      }else{
        return "未知"
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.humanList.length <= 0){
            this.$modal.msgWarning("请添加人设");
            return;
          }
          if(this.aiuiList.length <= 0){
            this.$modal.msgWarning("请添加aiui配置");
            return;
          }
          if(this.modelList.length <= 0){
            this.$modal.msgWarning("请添加大模型");
            return;
          }

          if (this.form.uid == undefined) {
            insertTenantAuthAi(this.form).then(response => {
              this.$modal.msgSuccess("添加成功");
              this.open = false;
              this.getList();
            })
          }else{
            let data = this.form;
            //提示
            const message = "<div>确定要修改配置信息？</div>"
            this.$modal.confirm(message).then(function() {
              return updateAiUiConfig(data);
            }).then(() => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            })
          }
        }
      })
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      let data = {};
      data.uid = row.uid;
      //提示
      const message = "<div>确定要删除租户权限: <span>" + row.tenantName + " ?</span></div>"+
        "<span style='margin-top: 20px'>应用ID:</span> &nbsp;&nbsp;&nbsp;" + row.uid
      this.$modal.confirm(message).then(function() {
        return deleteTenantAuthAi(data);
      }).then(() => {
        this.$modal.msgSuccess("删除成功");
        this.getList()
      })
    },
  }
};
</script>
